-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rich text: preserve white space should strip \r #58805
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core SVNCore Committers: Use this line as a base for the props when committing in SVN:
GitHub Merge commitsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +10 B (0%) Total Size: 1.71 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have confirmed that this issue is resolved on Windows OS and Chrome browsers where the issue occurred. After debugging according to the test procedure in this comment, it appears that \r
is removed as expected.
To make it more clear, what do you think about using variables, for example:
Diff
diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js
index aa7911bc90..e40f323692 100644
--- a/packages/rich-text/src/create.js
+++ b/packages/rich-text/src/create.js
@@ -9,7 +9,11 @@ import { select } from '@wordpress/data';
import { store as richTextStore } from './store';
import { createElement } from './create-element';
import { mergePair } from './concat';
-import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP } from './special-characters';
+import {
+ OBJECT_REPLACEMENT_CHARACTER,
+ CARRIAGE_RETURN,
+ ZWNBSP,
+} from './special-characters';
import { toHTMLString } from './to-html-string';
import { getTextContent } from './get-text-content';
@@ -408,7 +412,10 @@ function collapseWhiteSpace( element, isRoot = true ) {
:...skipping...
diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js
index aa7911bc90..e40f323692 100644
--- a/packages/rich-text/src/create.js
+++ b/packages/rich-text/src/create.js
@@ -9,7 +9,11 @@ import { select } from '@wordpress/data';
import { store as richTextStore } from './store';
import { createElement } from './create-element';
import { mergePair } from './concat';
-import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP } from './special-characters';
+import {
+ OBJECT_REPLACEMENT_CHARACTER,
+ CARRIAGE_RETURN,
+ ZWNBSP,
+} from './special-characters';
import { toHTMLString } from './to-html-string';
import { getTextContent } from './get-text-content';
@@ -408,7 +412,10 @@ function collapseWhiteSpace( element, isRoot = true ) {
export function removeReservedCharacters( string ) {
// with the global flag, note that we should create a new regex each time OR reset lastIndex state.
return string.replace(
- new RegExp( `[${ ZWNBSP }${ OBJECT_REPLACEMENT_CHARACTER }]`, 'gu' ),
+ new RegExp(
+ `[${ ZWNBSP }${ OBJECT_REPLACEMENT_CHARACTER }${ CARRIAGE_RETURN }]`,
+ 'gu'
+ ),
''
);
}
diff --git a/packages/rich-text/src/special-characters.js b/packages/rich-text/src/special-characters.js
index a05f614daf..980a6354fc 100644
--- a/packages/rich-text/src/special-characters.js
+++ b/packages/rich-text/src/special-characters.js
@@ -3,6 +3,11 @@
*/
export const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';
+/**
+ * Carriage Return.
+ */
+export const CARRIAGE_RETURN = '\u000d';
+
/**
* Zero width non-breaking space, used as padding in the editable DOM tree when
* it is empty otherwise.
I added it as a constant with a comment, but it's not reusable anywhere so I just left it in place |
Flaky tests detected in 4eb55ee. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7826910436
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
A slight concern is that older Mac OS (OS 9) only used carriage returns.
Windows, Linux and Classic Mac OS Line Endings
However, there are almost no users using this OS anymore, and the block editor itself probably doesn't work, so I don't think it's necessary to consider it.
Let's see and we can adjust if necessary, at least this fixes an important problem. |
for what its worth, HTML does not respect carriage returns. when interpreting HTML, first, all |
That's good to know! |
What?
\n
on its own is internally interpreted as a BR element. On Windows this causes\r
characters to linger.It also didn't make sense that content would be different depending on the OS, because it can be rendered in any environment and should be the same everywhere. I think the decision to move to
BR
element for pre makes even more sense now. If we ever serialise rich text to use character line breaks, perhaps it should always be serialised to\r\n
for OS compatibility. But we don't need to do that now because we always serialise to a BR element.Why?
See #58659
How?
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast